package com.jtpay.sdk.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jtpay.sdk.R;
import com.jtpay.sdk.activity.PlatformConfigActivity;
import com.jtpay.sdk.model.PlatformInfo;
import com.jtpay.sdk.util.JftConstants;
import com.jtpay.sdk.util.JftLogUtil;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/6/18 0018.
 */

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder> {
    private ArrayList<PlatformInfo> mPlatformInfos;

    public RecycleViewAdapter(ArrayList<PlatformInfo> platformInfos) {
        mPlatformInfos = platformInfos;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_platform_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.getNameText().setText(mPlatformInfos.get(position).getName());
    }


    @Override
    public int getItemCount() {
        return mPlatformInfos.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView mNameText;

        public ViewHolder(View view) {
            super(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    JftLogUtil.log("onClick");
                    startActivity(mNameText.getText().toString());
                }
            });
            mNameText = (TextView) view.findViewById(R.id.text_platform);
        }

        public TextView getNameText() {
            return mNameText;
        }

        private void startActivity(String text) {
            Context context = mNameText.getContext();
            int size = mPlatformInfos.size();
            for (int i = 0; i < size; i++) {
                PlatformInfo info = mPlatformInfos.get(i);
                String name = info.getName();
                if (name.equals(text)) {
                    Intent intent = new Intent(context, PlatformConfigActivity.class);
                    intent.putExtra(JftConstants.JUN_FU_TONG_PLAT_FORM_NAME, text);
                    intent.putExtra(JftConstants.JUN_FU_TONG_USER_CODE, info.getUserCode());
                    intent.putExtra(JftConstants.JUN_FU_TONG_COM_KEY, info.getComkey());
                    context.startActivity(intent);
                    break;
                }
            }

        }
    }

}
